package com.zr.task.adapter;

import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.RequiresApi;

import com.zh.baselibrary.base.BaseRecyclerAdapter;
import com.zh.model.task.home.CabinetBean;
import com.zr.task.R;
import com.zr.task.dialog.CabDialog;

import java.util.List;

public class HomeCabinetAdapter extends BaseRecyclerAdapter<CabinetBean> {
    //柜子
    private static final int ITEM_TYPE_CABINET = 0;
    //主机
    private static final int ITEM_TYPE_HOST = 1;

    public static final int CABINET_TYPE_3 = 3;

    private Integer count = 0;

    public HomeCabinetAdapter(List<CabinetBean> mData) {
        super(mData);
    }



    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    public int getItemCount() {
        return count;
    }
    public  List<CabinetBean> getItem(){
        return mData;
    }

    @Override
    public int getItemViewType(int position) {
        if (count <= CABINET_TYPE_3) {
            if (position == 0)
                return ITEM_TYPE_HOST;
            return ITEM_TYPE_CABINET;
        } else {
            if (position == 1)
                return ITEM_TYPE_HOST;
            return ITEM_TYPE_CABINET;
        }
    }

    @Override
    protected int getLayoutId(int viewType) {
        if (viewType == ITEM_TYPE_CABINET)
            return R.layout.home_cabinet_item_layout;
        return R.layout.home_host_item_layout;
    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void bindData(BaseViewHolder holder, int position, CabinetBean cabinetBean) {
        if (getItemViewType(position) == ITEM_TYPE_CABINET) {
            if (cabinetBean != null) {
                setItemText(holder.getView(R.id.tv_subject), cabinetBean.getGradeClassName()+ "  " + cabinetBean.getSubjectName());
                String teacherName1 = cabinetBean.getTeacherName();
                int count = cabinetBean.getCount();
                if (count != -1 && count != 0) {
                    setItemText(holder.getView(R.id.tv_teacher_count), teacherName1 + "(" + count + ")");
                } else {
                    setItemText(holder.getView(R.id.tv_teacher_count), teacherName1);
                }
                holder.getView(R.id.detail_one).setVisibility(View.VISIBLE);

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new CabDialog(mContext, R.style.DialogTheme, cabinetBean).show();
                    }
                });

            }else {
                setItemText(holder.getView(R.id.tv_teacher_count), "");
                setItemText(holder.getView(R.id.tv_subject), "");
                holder.getView(R.id.detail_one).setVisibility(View.GONE);
            }
            RelativeLayout llSubject = (RelativeLayout) holder.getView(R.id.ll_subject);
            if (count <= CABINET_TYPE_3) {
                llSubject.setBackground(mContext.getResources().getDrawable(R.drawable.cabinet));
            } else {
                if (position == 0) {
                    llSubject.setBackground(mContext.getResources().getDrawable(R.drawable.cabinet_left));
                } else if (position == getItemCount() / 3 - 1) {
                    llSubject.setBackground(mContext.getResources().getDrawable(R.drawable.cabinet_right));
                } else {
                    llSubject.setBackground(mContext.getResources().getDrawable(R.drawable.cabinet));
                }
            }

        } else {
            if (count <= CABINET_TYPE_3)
                ((ImageView) holder.getView(R.id.iv_host)).setImageResource(R.drawable.host);
            else
                ((ImageView) holder.getView(R.id.iv_host)).setImageResource(R.drawable.host_2);
        }

    }
}
